#pragma once
#include "wx\thread.h"
#include	"wx/socket.h"
#include	"../saver/datsaver.h"
//#include	"../datamanager.h"
#include	"algorithm.h"

#pragma once
#include	"../plotframehandler.h"
class PlotFrameHandler;

class Instrument :	public wxThread
{
public:
	enum ConnFlags
	{
		CONNECTION_OK = 0,
		CONNECTION_LOST	=	1,
		CONNECTION_RECOVERED	=	2
	}ConnFlags;
	
public:
	Instrument(void);
	wxTextCtrl *pOutLog;
	wxTextCtrl *pOutLog2;
	virtual void *Entry(void)
	{
		return 0;
	}
	virtual void StartDevice(wxString FileName,wxString FilePath,bool file);
		
	virtual void PassParameters (void*)
	{
	}
	bool ToggleShow(void);
	void SetHIMTrigger(int Trig)
	{
		CurrentEvents.HIMTrigg = Trig;
	};
	void SetAEnimaTrigger(int Trig)
	{
		CurrentEvents.AEnimaTrigg = Trig;
		//CurrentEvents.
	}
	
	float GetFrequency(void){return Info.EstimatedFrequency;}
	virtual void StopDevice(void);
	
	virtual int OpenPort(const char *PortName, const char *val2, const char *val3){return -1;};
	
	void RegisterSocket(wxSocketBase *pSock)
	{
		int		Len;
		char	pTemp[2048];
		sock = pSock;
		if(pAlgorithm != NULL && sock != NULL)
		{
			Len = pAlgorithm->GetData(pTemp);
			wxASSERT_MSG(Len%sizeof(BCIMessage)==0,_T("Algorihm:: Algorithms should only send BCIMessage Structs"));
			if(Len > 0) 
			{
				for(int i=0;i<Len;i+=sizeof(BCIMessage))	
				{
					sock->Write(&pTemp[i],sizeof(BCIMessage));
				}
			}
		}
	}
	//wxString* GetLabels(void) {return Labels;}

	wxString	GetLastMessage(void)
	{	
		wxString Ret = LastMessage; 
		LastMessage = wxString::Format(_T(""));
		return Ret;
	}
	void RegisterAlgorithm(Algorithm *pAlgo);

	void	CheckFrequency(void);
	void	PutSample(SampleStruct *Signal);
	void	StartAcquisition(void)
	{
		//wxThread::Run();
		int Result;
		memset(&Info,0,sizeof(Info));
		Info.BeginTime = GetTickCount();
		Info.Errors = 0;
		Info.EstimatedFrequency = 0;
		Info.ReceivedSamples=0;
		Result = Create();
		Run();
	}
	int		Close(void);


	void SetAcqInfo(wxStaticText *pFreq, wxStaticText *pTime, wxStaticText *pSample, wxStaticText *pError);
	virtual int	GetBatteryLevel(void){return -1;}
	virtual int	SetAcqParams(){return 0;};
	bool	EnableFeedback,	EnableClassification;
	char	AcqName[256],ClassName[256];
	wxString	MyName;
	virtual void CheckImpedance(void){};
	virtual void Configure(void){};
	bool ImpChFlag;
public:
	
	~Instrument(void);
	PlotFrameHandler	*pPlotFrame;
	wchar_t *yLabel;
protected:
	EventStruct	CurrentEvents;
	void	IncErrorCounter(void) {Info.Errors++;}
	void	IncLostPacketCounter(void) {Info.LostPackets++;}
	void	CheckFlags(EventStruct *pStruct);
	
	void		SetMessage(wxString String)
	{
		LastMessage = String;
	}
	
/*	int			SavesCount;
	wxString	SavesFilename;
*/
	

	void	Initialize(int Chann,int Frequency,int PlotLength);
	int		Channels;

	

	wxSocketBase	*sock;
	wxString		Labels[MAX_CHANNELS];
	float			Maximum[MAX_CHANNELS];
	float			Minimum[MAX_CHANNELS];
	CDatSaver		DataSaver;
	Algorithm		*pAlgorithm;
	PlotFrameHandler	*pPlot;
	//DataManager		*pDataSplit;
	bool			IsRunning;
	bool			Esci;
	double			Resolution,DigitalOffset,DigitalMax,DigitalMin;	
	int				SampleCounter,TeoreticalSampleFreq;
	AcqInfo			Info;
	long int		LastFreqCheckTime;
	int				SampleFreq;
	bool			file;
	wxString	LastMessage;
public:
	void	Rescale(SampleStruct *Signal);
	int		SetConnectionFlag(enum ConnFlags Flag);
	wxStaticText *myElapTime;
	wxStaticText *myFreq;
	wxStaticText *mySample;
	wxStaticText *myError;



	
};

